#include "SpriteList.h"
#include "SpriteID.h"

SpriteList::SpriteList()
{
	D3DXMatrixIdentity(&spriteMat);
	D3DXMatrixIdentity(&spriteScale);
	D3DXMatrixIdentity(&spriteRotate);
	D3DXMatrixIdentity(&spriteTranslate);
}

void SpriteList::Init(LPDIRECT3DDEVICE9 device)
{
	char* initList[] =
	{
		BUTTON_PATH,
		BUTTON_SELECTED_PATH,
		BULLET_PATH,
		ENEMY_PATH,
		PLAYER_PATH,
		GAME_BACKGROUND_PATH,
		MENU_BACKGROUND_PATH,
	};
	for(int i=0;i<sizeof(initList)/sizeof(initList[0]);++i)
	{
		IDirect3DTexture9* add;
		D3DXIMAGE_INFO addAlso;
		D3DXCreateTextureFromFileEx(device, initList[i], D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), &addAlso, 0, &add);
		texList.add(add);
		infoList.add(addAlso);
	}
}

SpriteList::~SpriteList()
{
	for(int i=0;i<texList.Size();++i)
		if(texList.get(i))
			texList.get(i)->Release();
}

void SpriteList::draw(ID3DXSprite* sprite, DrawStruct info)
{
	D3DXMatrixIdentity(&spriteMat);
	D3DXMatrixIdentity(&spriteScale);
	D3DXMatrixIdentity(&spriteRotate);
	D3DXMatrixIdentity(&spriteTranslate);

	D3DXMatrixTranslation(&spriteTranslate, info.pos.x, info.pos.y, 0.0f);
	D3DXMatrixRotationZ(&spriteRotate, D3DXToRadian(info.orientation));
	D3DXMatrixScaling(&spriteScale, info.scale.x, info.scale.y, 1.0f);
	
	D3DXMatrixMultiply(&spriteScale, &spriteScale, &spriteRotate);		//Multiply scale and rotation, stored in scale
	D3DXMatrixMultiply(&spriteMat, &spriteScale, &spriteTranslate);		//Multiply scale and translation, stored in world
	
	sprite->SetTransform(&spriteMat);
	
	int Tex = info.Tex;
	D3DXIMAGE_INFO size = infoList.get(Tex);

	if(info.DrawFromCenter)
		sprite->Draw(texList.get(Tex), 0, &D3DXVECTOR3( size.Width/2.0f, size.Height/2.0f, 0.0f), 0, D3DCOLOR_XRGB(255,255,255));
	else
		sprite->Draw(texList.get(Tex), 0, &D3DXVECTOR3( 0.0f, 0.0f, 0.0f), 0, D3DCOLOR_XRGB(255,255,255));
}